﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Marmat.Foundation;

namespace Marmat.FrameWork
{
    /// <summary>
    /// 记录选择窗体的扩展支持
    /// </summary>
    public static class SelectRecordFormExtension
    {

        /// <summary>
        /// 扩展选择窗体的列构造器
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="expression"></param>
        public static void ColumnBuilder<TEntity>(this ISelectionRecordBasicForm instance, System.Linq.Expressions.Expression<Func<TEntity, object>> expression) where TEntity : class
        {
            instance.ColumnBuilder<TEntity>(expression, null);
        }

        /// <summary>
        /// 扩展选择窗体的列构造器
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="expression"></param>
        /// <param name="action"></param>
        public static void ColumnBuilder<TEntity>(this ISelectionRecordBasicForm instance, System.Linq.Expressions.Expression<Func<TEntity, object>> expression, Action<SelectionRecordColumn> action) where TEntity : class
        {
            List<string> columns = new List<string>();
            if (expression.Body is System.Linq.Expressions.UnaryExpression unary)
            {
                if (unary.Operand is System.Linq.Expressions.MemberExpression member)
                    columns.Add(member.Member.Name);
            }
            else if (expression.Body is System.Linq.Expressions.MemberExpression member)
                columns.Add(member.Member.Name);
            else if (expression.Body is System.Linq.Expressions.NewExpression newExpression)
            {
                foreach (var item in newExpression.Arguments)
                {
                    if (item is System.Linq.Expressions.MemberExpression item_member)
                        columns.Add(item_member.Member.Name);
                }
            }
            ColumnBuilder(instance, columns, action);
        }

        /// <summary>
        /// 扩展选择窗体的列构造器
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="columns"></param>
        public static void ColumnBuilder(this ISelectionRecordBasicForm instance, params string[] columns)
        {
            ColumnBuilder(instance, columns, null);
        }

        /// <summary>
        /// 扩展选择窗体的列构造器
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="columns"></param>
        /// <param name="action"></param>
        public static void ColumnBuilder(this ISelectionRecordBasicForm instance, IEnumerable<string> columns, Action<SelectionRecordColumn> action)
        {
            void columnBuilder(SelectionRecordColumn column)
            {
                column.Cancel = !columns.Contains(column.ColumnName);
                column.Index = Array.IndexOf(columns.ToArray(), column.ColumnName);
                action?.Invoke(column);
            }
            instance.ColumnBuilder = columnBuilder;
        }
    }
}
